package com.system.ras;

import java.net.URLDecoder;

import common.base.util.StringUtil;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

/*******************************************************
 *  
 * Description:自定义RSA处理类 
 * 如需增加Spring Security 另外的加密方式，重新定义此类<br/> 
 * 
 * ********************************************************/
public class RSAPasswordEncoder{
	/*****************************************
	 * * Description:明文加密 <br/>
	 * * @return * @param rawPass * 密码
	 ******************************************/
	public String encodePassword(String rawPass, Object salt) {
		String encoder = "";
		try {
			encoder = MpbSecureRSAUtil.decryptString(rawPass);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return encoder;
	}

	/*****************************************
	 * * Description:验证密码是否有效主要是此方法的使用<br/>
	 * * * @return boolean
	 ******************************************/
	public boolean isPasswordValid(String encPass, String rawPass, Object salt) {
		if (!StringUtil.isEmpty(encPass) || encPass.length() == 0) {
			return false;
		}
		String decPsw = MpbSecureRSAUtil.decryptString(encPass);
		String inpdecPsw = MpbSecureRSAUtil.decryptStringByJs(rawPass);
		try {
			decPsw = URLDecoder.decode(decPsw, "UTF-8");
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
		return inpdecPsw.equals(decPsw);
	}

	/*****************************************
	 * * Description:私钥解密 <br/>
	 * * * @return
	 ******************************************/
	public String decryptPassword(String rawPass, Object salt) {
		String encoder = "";
		try {
			encoder = MpbSecureRSAUtil.decryptString(rawPass);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return encoder;
	}

	/*****************************************
	 * * Description:Base64解密 <br/>
	 * * @param key * @return * @throws Exception
	 ******************************************/
	public static byte[] decoderBase64(String key) throws Exception {
		return (new BASE64Decoder()).decodeBuffer(key);
	}      
	/*****************************************
	 *Description:Base64加密 <br/>      
	 *@param key      
	 *@return      
	 *@throws Exception      
	 *******************************************/     
	public static String encoderBase64(byte[] key) throws Exception {
		return (new BASE64Encoder()).encodeBuffer(key);
	}
}